package org.codingq92.basic.thread;

import java.util.concurrent.TimeUnit;

public class DeadLock {
    public static void main(String[] args) {
        Object aLock = new Object();
        Object bLock = new Object();
        new Thread(()->{
            synchronized (aLock){
                System.out.println("t1拿到a锁，开始获取b锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (bLock){
                    System.out.println("t1拿到b锁");
                }
            }
        }).start();
        new Thread(()->{
            synchronized (bLock){
                System.out.println("t2拿到b锁，开始获取a锁");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (aLock){
                    System.out.println("t2拿到a锁");
                }
            }
        }).start();
    }
}
